home *** CD-ROM | disk | FTP | other *** search
/ Mac Easy 2010 May / Mac Life Ubuntu.iso / casper / filesystem.squashfs / usr / lib / rhythmbox / plugins / rb / Coroutine.pyc (.txt) < prev    next >
Encoding:
Python Compiled Bytecode  |  2009-04-20  |  2.0 KB  |  60 lines

  1. # Source Generated with Decompyle++
  2. # File: in.pyc (Python 2.6)
  3.  
  4.  
  5. class Coroutine:
  6.     '''A simple message-passing coroutine implementation. 
  7. \tNot thread- or signal-safe. 
  8. \tUsage:
  9. \t\tdef my_iter (plexer, args):
  10. \t\t\tsome_async_task (..., callback=plexer.send (tokens))
  11. \t\t\tyield None
  12. \t\t\ttokens, (data, ) = plexer.receive ()
  13. \t\t\t...
  14. \t\tCoroutine (my_iter, args).begin ()
  15. \t'''
  16.     
  17.     def __init__(self, iter, *args):
  18.         self._continuation = iter(self, *args)
  19.         self._executing = False
  20.  
  21.     
  22.     def _resume(self):
  23.         if not self._executing:
  24.             self._executing = True
  25.             
  26.             try:
  27.                 self._continuation.next()
  28.                 while self._data:
  29.                     self._continuation.next()
  30.             except StopIteration:
  31.                 pass
  32.             finally:
  33.                 self._executing = False
  34.  
  35.         
  36.  
  37.     
  38.     def clear(self):
  39.         self._data = []
  40.  
  41.     
  42.     def begin(self):
  43.         self.clear()
  44.         self._resume()
  45.  
  46.     
  47.     def send(self, *tokens):
  48.         
  49.         def callback(*args):
  50.             self._data.append((tokens, args))
  51.             self._resume()
  52.  
  53.         return callback
  54.  
  55.     
  56.     def receive(self):
  57.         return self._data.pop(0)
  58.  
  59.  
  60.